/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.sessionbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.RegisteredUser;
import com.blog.city.checkthedep.entities.Comment;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Breath
 */
@Stateless
public class CommentFacade extends AbstractFacade<Comment> implements CommentFacadeLocal {

    @PersistenceContext(unitName = "com.blog.city_CheckTheDep_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    /**
     * 
     * @return
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * 
     * @param em
     */
    protected void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * 
     */
    public CommentFacade() {
        super(Comment.class);
    }

    /**
     * 
     * @return
     */
    @Override
    public List<Comment> latestComments() {
        List<Comment> latestComments = null;
        if ((List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").getResultList() != null) {
            latestComments = (List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").getResultList();
        }
        if (latestComments.size() >= 10) {
            return (List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").setMaxResults(5).getResultList();
        } else if (latestComments.size() < 10 && latestComments.size() >= 5) {
            return (List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").setMaxResults(3).getResultList();
        } else if (latestComments.size() < 5 && latestComments.size() >= 3) {
            return (List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").setMaxResults(2).getResultList();
        } else {
            return (List<Comment>) getEntityManager().createQuery("SELECT a FROM Comment a ORDER BY id DESC").setMaxResults(1).getResultList();
        }
    }

    /**
     * 
     * @param comm
     * @return
     */
    @Override
    public boolean exists(Comment comm) {
        List<Comment> allComments = null;
        allComments = (List<Comment>) getEntityManager().createQuery("SELECT c FROM Comment c ").getResultList();
        for (int i = 0; i < allComments.size(); i++) {
            if (allComments.get(i).equals(comm)) {
                return true;
            }
        }
        return false;
    }

    /**
     * This method is not used because we had problems with deserializing the object. Could not get back the actual list. The problem was also in 
     * Arquillian tests.
     * @param regId
     * @return
     */
    @Override
    public List<Comment> findByRegUserIdNotUsed(RegisteredUser regId) {
        return regId.getCommentList();
    }

    /**
     * 
     * @param regId
     * @return
     */
    @Override
    public List<Comment> findByRegUserId(RegisteredUser regId) {
        return (List<Comment>) getEntityManager().createQuery("SELECT c FROM Comment c WHERE c.registeredUserId= :registeredUserId").setParameter("registeredUserId", regId).getResultList();
    }

    /**
     * 
     * @return
     */
    @Override
    public List<Comment> findAllCommentsForTest() {
        return (List<Comment>) this.getEntityManager().createQuery("SELECT c FROM Comment c ").getResultList();
    }
    
    @Override
    public List<Comment> findByArticle(Article artId) {
        return (List<Comment>) getEntityManager().createQuery("SELECT c FROM Comment c WHERE c.articleId= :articleId").setParameter("articleId", artId).getResultList();
    }

}
